﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ChgDoc.Entity;
using System.Text;

namespace ChgDoc.Edit
{
    [AdminPage( "Page Layout", "Admin" )]
    public partial class ArticleEdit : ChgDoc.Edit.BasePage
    {
        protected override IEnumerable<FormRule> GetFormRules()
		{
			return new List<FormRuleMapping<Article>>()
		                {
		                    new FormRuleMapping<Article>( txtTitle, "Title", FormRuleCondition.NotNull, (s,v) => s.Title = (string)v),
                            new FormRuleMapping<Article>( txtBody, "Body", FormRuleCondition.None, (s,v) => s.Body = (string)v),
                            new FormRuleMapping<Article>( drpTemplate, "Template", FormRuleCondition.None, (s,v) => s.TemplateId = long.Parse((string)v)) 
		                };
		}
		
		protected void Page_Load( object sender, EventArgs e )
		{
			if (IsPostBack)
				return;


            drpTemplate.DataSource = Template.ReadTopLevel();
            drpTemplate.DataTextField = "Name";
            drpTemplate.DataValueField = "Id";
            drpTemplate.DataBind();

            drpLocation.DataSource = Location.ReadAll();
            drpLocation.DataTextField = "Name";
            drpLocation.DataValueField = "Id";
            drpLocation.DataBind();

            drpTag.DataSource = Tag.ReadAll();
            drpTag.DataValueField = "Id";
            drpTag.DataTextField = "Name";
            drpTag.DataBind();

            drpListAttributes.DataSource = EntityAttribute.DistinctCodesFor(EntityType.Article).Union(new List<string>() { "New/Value" });
            drpListAttributes.DataBind();


			if (!HasParam( "Id" ))
				return;

            Article load = Article.Read( LongParam( "Id" ) );
			txtTitle.Text = load.Title;
			txtBody.Text = load.Body;
            drpTemplate.SelectedValue = load.TemplateId.ToString();

            litScript.Text = "<script type=\"text/javascript\">article.id = " + load.Id + "; article.listAll();</script>";
        
		}

		protected void btnSave_click(object sender, EventArgs e)
		{
            Article model = new Article() { Id = LongParam( "Id" ) };

            if (!PopulateModelFromControls<Article>( model ))
                return;

			model.Save();
            Config.ClearCaches();

            DoReturnUrl();
		}
	}
    
}